package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.TransferManagement;
import com.gxa.mapper.TransferModeMapper;
import com.gxa.service.TransferModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class TransferModeServiceImpl implements TransferModeService {

    @Autowired
    private TransferModeMapper transferModeMapper;

    /**
     * 添加转运方式
     * @param transferManagement
     * @return
     */
    @Override
    @Transactional
    public R addTransferMode(TransferManagement transferManagement) {
        R r = new R();
        try{
            //先判断该转运方式是否已存在
            TransferManagement selectedTransferManagement = transferModeMapper.selectTransferModeByName(transferManagement.getTransferMode());
            if(selectedTransferManagement == null){
                //要添加的转运方式不存在，进行添加
                transferModeMapper.insertTransferMode(transferManagement);
                r.setCode(200);
                r.setMsg("添加成功！");
            }else {
                r.setCode(1001);
                r.setMsg("添加失败，要添加的转运方式已存在！");
            }
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,添加失败,请联系管理员");
        }
        return r;
    }

    /**
     * 根据ID删除转运方式
     * @param id
     * @return
     */
    @Override
    public R deleteTransferMode(int id) {
        R r = new R();
        try {
            transferModeMapper.deleteTransferMode(id);
            r.setCode(200);
            r.setMsg("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,删除失败,请联系管理员");
        }
        return r;
    }

    /**
     * 分页查询所有转运方式
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R queryAllTransferModesByPage(int currentPage, int pageSize) {
        R r = new R();
        try {
            Page<TransferManagement> page = PageHelper.startPage(currentPage,pageSize);
            List<TransferManagement> transferManagementList = transferModeMapper.selectAllTransferModes();
            int count = (int)page.getTotal();
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setCount(count);
            r.setData(transferManagementList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,查询失败,请联系管理员");
        }
        return r;
    }

    /**
     * 修改转运方式
     * @param transferManagement
     * @return
     */
    @Override
    @Transactional
    public R updateTransferMode(TransferManagement transferManagement) {
        R r = new R();
        try {
            //先删除该转运方式
            transferModeMapper.deleteTransferMode(transferManagement.getId());
            //再添加修改后的转运方式
            transferModeMapper.insertTransferModeWithFixedId(transferManagement);
            r.setCode(200);
            r.setMsg("修改成功！");
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,修改失败,请联系管理员");
        }
        return r;
    }
}
